﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using blackhouse.UI;
using System.Text;

namespace blackhouse.Web.UI
{
    public class PageCounterControl : Control
    {

        #region Fields

        private WebPageCounterAdapter adapter_priv;

        #endregion

        #region Properties

        public WebPageCounterAdapter Controller
        {
            get
            {
                if (this.adapter_priv == null)
                    this.adapter_priv = new WebPageCounterAdapter(this);
                return this.adapter_priv;
            }
            set { this.adapter_priv = value; }
        }

        public int CurrentPage
        {
            get { return this.Controller.CurrentPage; }
            set { this.Controller.CurrentPage = value; }
        }

        public int ItemsOnPage
        {
            get { return this.Controller.ItemsOnPage; }
            set { this.Controller.ItemsOnPage = value; }
        }

        /// <summary>
        /// Format HTML standardowej, ostatniej strony, np.: <![CDATA[ <a href="Default.aspx?page={0}">{0}</a>]]>. {0} jest to numer aktualnie budowanej strony
        /// </summary>
        public string FormatLastPage { get; set; }

        /// <summary>
        /// Format HTML standardowej, następnej strony, np.: <![CDATA[ <a href="Default.aspx?page={0}">{0}</a>]]>. {0} jest to numer aktualnie budowanej strony
        /// </summary>
        public string FormatNextPage { get; set; }

        /// <summary>
        /// Format HTML standardowej strony, np.: <![CDATA[ <a href="Default.aspx?page={0}">{0}</a>]]>. {0} jest to numer aktualnie budowanej strony
        /// </summary>
        public string FormatPage { get; set; }

        /// <summary>
        /// Format HTML standardowej, wybranej strony, np.: <![CDATA[ <a href="Default.aspx?page={0}">{0}</a>]]>. {0} jest to numer aktualnie budowanej strony
        /// </summary>
        public string FormatSelectedPage { get; set; }

        #endregion

        #region Methods

        public void SetNodes(IEnumerable<PageCounterNode> nodes)
        {
            StringBuilder builder = new StringBuilder();
            foreach (PageCounterNode node in nodes)
            {
                if (node.Type == PageCounterNode.PageCounterNodeType.Page)
                    builder.AppendFormat(FormatPage, node.PageNumber);
                if (node.Type == PageCounterNode.PageCounterNodeType.SelectedPage)
                    builder.AppendFormat(this.FormatSelectedPage, node.PageNumber);
                if (node.Type == PageCounterNode.PageCounterNodeType.LastPage)
                    builder.AppendFormat(this.FormatLastPage, node.PageNumber);
                if (node.Type == PageCounterNode.PageCounterNodeType.NextPage)
                    builder.AppendFormat(this.FormatNextPage, node.PageNumber);
            }
            this.Controls.Add(
                new LiteralControl()
                {
                    Text = builder.ToString()
                });
        }

        #endregion

        #region IPageCounter Members

        public int GetCurrentPage()
        {
            return this.CurrentPage;
        }

        public int GetItemsOnPage()
        {
            return this.ItemsOnPage;
        }

        public void SetTotalItems(int itemsCount)
        {
            this.Controller.SetTotalItems(itemsCount);
        }

        #endregion
    }
}